# ============================================================================
# 统一配置管理模块
# ============================================================================
# 提供项目全局配置变量的统一管理
# ============================================================================

# 项目根目录（必须最先定义）
if(NOT DEFINED CMAKE_NFSHM_SOURCE_DIR)
    set(CMAKE_NFSHM_SOURCE_DIR ${CMAKE_SOURCE_DIR})
endif()

# 项目版本信息（可以从外部传入或从文件读取）
if(NOT DEFINED PROJECT_VERSION)
    set(PROJECT_VERSION "1.0.0")
endif()

# 构建工具标志
if(NOT DEFINED CMAKE_BUILD_TOOLS)
    set(CMAKE_BUILD_TOOLS OFF CACHE BOOL "是否构建工具项目")
endif()

# 游戏项目构建标志
if(NOT DEFINED CMAKE_BUILD_GAME)
    set(CMAKE_BUILD_GAME "" CACHE STRING "要构建的游戏项目名称（如：LieRen）")
endif()

# 链接时优化标志（LTO）
if(NOT DEFINED CMAKE_BUILD_LTO)
    set(CMAKE_BUILD_LTO OFF CACHE BOOL "是否启用链接时优化（LTO）")
endif()

# 第三方库编译选项（默认不编译）
if(NOT DEFINED CMAKE_BUILD_MYSQLPP)
    set(CMAKE_BUILD_MYSQLPP OFF CACHE BOOL "是否编译 mysqlpp 库")
endif()

if(NOT DEFINED CMAKE_BUILD_SQLITE3)
    set(CMAKE_BUILD_SQLITE3 OFF CACHE BOOL "是否编译 sqlite3 库")
endif()

if(NOT DEFINED CMAKE_BUILD_NANOPB)
    set(CMAKE_BUILD_NANOPB OFF CACHE BOOL "是否编译 nanopb 库")
endif()

if(NOT DEFINED CMAKE_BUILD_NF3RDCOMMON)
    set(CMAKE_BUILD_NF3RDCOMMON OFF CACHE BOOL "是否编译 NF3rdCommon 库")
endif()

if(NOT DEFINED CMAKE_BUILD_NFLUABIND)
    set(CMAKE_BUILD_NFLUABIND OFF CACHE BOOL "是否编译 NFLuaBind 库")
endif()

# 输出目录配置（根据构建类型设置）
# 库文件放在各自的构建目录下，便于管理和清理
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "静态库输出目录" FORCE)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "动态库输出目录" FORCE)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Release" CACHE PATH "可执行文件输出目录" FORCE)
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "静态库输出目录" FORCE)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "动态库输出目录" FORCE)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Debug" CACHE PATH "可执行文件输出目录" FORCE)
elseif(CMAKE_BUILD_TYPE STREQUAL "DynamicRelease")
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "静态库输出目录" FORCE)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/DynamicRelease" CACHE PATH "动态库输出目录" FORCE)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/DynamicRelease" CACHE PATH "可执行文件输出目录" FORCE)
elseif(CMAKE_BUILD_TYPE STREQUAL "DynamicDebug")
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "静态库输出目录" FORCE)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/DynamicDebug" CACHE PATH "动态库输出目录" FORCE)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/DynamicDebug" CACHE PATH "可执行文件输出目录" FORCE)
else()
    # 默认配置
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "静态库输出目录" FORCE)
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "动态库输出目录" FORCE)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "可执行文件输出目录" FORCE)
endif()

# 确保链接器能够找到库文件（使用 CMAKE_ARCHIVE_OUTPUT_DIRECTORY 作为链接器搜索路径）
# 注意：Windows 平台的库路径由 BuildTypeConfig.cmake 统一管理（通过 LIB_DIR 变量）
# 这里只为 UNIX 平台设置，避免重复设置
if(UNIX)
    link_directories(${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
endif()

# 安装目录配置
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX "${CMAKE_NFSHM_SOURCE_DIR}/Deploy" CACHE PATH "安装路径前缀" FORCE)
endif()

# 根据构建类型设置二进制安装子目录
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(BIN_INSTALL_DIR "Bin/Release")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(BIN_INSTALL_DIR "Bin/Debug")
elseif(CMAKE_BUILD_TYPE STREQUAL "DynamicRelease")
    set(BIN_INSTALL_DIR "Bin/DynamicRelease")
elseif(CMAKE_BUILD_TYPE STREQUAL "DynamicDebug")
    set(BIN_INSTALL_DIR "Bin/DynamicDebug")
else()
    set(BIN_INSTALL_DIR "Bin")
endif()

# 根据构建类型设置源二进制目录
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(SOURCE_BIN_DIR "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Release")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(SOURCE_BIN_DIR "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Debug")
elseif(CMAKE_BUILD_TYPE STREQUAL "DynamicRelease")
    set(SOURCE_BIN_DIR "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/DynamicRelease")
elseif(CMAKE_BUILD_TYPE STREQUAL "DynamicDebug")
    set(SOURCE_BIN_DIR "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/DynamicDebug")
endif()

# 游戏目录配置（优先使用环境变量）
if(DEFINED ENV{GAME_DIR} AND NOT "$ENV{GAME_DIR}" STREQUAL "")
    set(INSTALL_GAME_DIR "$ENV{GAME_DIR}")
elseif(DEFINED CMAKE_BUILD_GAME AND NOT CMAKE_BUILD_GAME STREQUAL "")
    set(INSTALL_GAME_DIR "${CMAKE_BUILD_GAME}")
else()
    set(INSTALL_GAME_DIR "")
endif()

# 输出配置信息
message(STATUS "========================================")
message(STATUS "项目配置信息")
message(STATUS "========================================")
message(STATUS "项目根目录: ${CMAKE_NFSHM_SOURCE_DIR}")
message(STATUS "构建类型: ${CMAKE_BUILD_TYPE}")
message(STATUS "构建工具: ${CMAKE_BUILD_TOOLS}")
if(CMAKE_BUILD_GAME AND NOT CMAKE_BUILD_GAME STREQUAL "")
    message(STATUS "游戏项目: ${CMAKE_BUILD_GAME}")
endif()
message(STATUS "安装目录: ${CMAKE_INSTALL_PREFIX}")
message(STATUS "二进制安装目录: ${BIN_INSTALL_DIR}")
if(INSTALL_GAME_DIR AND NOT INSTALL_GAME_DIR STREQUAL "")
    message(STATUS "游戏资源目录: ${INSTALL_GAME_DIR}")
endif()
message(STATUS "----------------------------------------")
message(STATUS "第三方库编译选项:")
message(STATUS "  mysqlpp: ${CMAKE_BUILD_MYSQLPP}")
message(STATUS "  sqlite3: ${CMAKE_BUILD_SQLITE3}")
message(STATUS "  nanopb: ${CMAKE_BUILD_NANOPB}")
message(STATUS "  NF3rdCommon: ${CMAKE_BUILD_NF3RDCOMMON}")
message(STATUS "  NFLuaBind: ${CMAKE_BUILD_NFLUABIND}")
message(STATUS "========================================")

